#include "si12.h"
#include "buzzer.h"

#if DEBUG

#include "debug_uart.h"

#endif



/********************************
 * 函数功能  ：SI12TCH写字节
 * 参数1     ：芯片寄存器地址
 * 参数2     ：写入寄存器的值
 *
*********************************/
void SI12TCH_WriteOneByte(u8 WriteAddr,u8 DataToWrite)
{
	si12tch_i2c_Start();
	si12tch_i2c_SendByte(SI12TCH_WRITE_ADDR);
	si12tch_i2c_WaitAck();
	si12tch_i2c_SendByte(WriteAddr);
	si12tch_i2c_WaitAck();
	si12tch_i2c_SendByte(DataToWrite);
	si12tch_i2c_WaitAck();
	si12tch_i2c_Stop();
	delay_ms(10);
}

/********************************
 * 函数功能 ：SI12TCH读字节
 * 参数     ：芯片寄存器地址
 * 返回值   ：芯片寄存器地址值
 *
*********************************/
u8 SI12TCH_ReadOneByte(u8 ReadAddr)
{
	u8 temp=0;
	si12tch_i2c_Start();
	si12tch_i2c_SendByte(SI12TCH_WRITE_ADDR);

	si12tch_i2c_WaitAck();
	si12tch_i2c_SendByte(ReadAddr);
	si12tch_i2c_WaitAck();
	si12tch_i2c_Stop();
	delay_xms(10);
	si12tch_i2c_Start();
	si12tch_i2c_SendByte(SI12TCH_READ_ADDR);
	si12tch_i2c_WaitAck();
	temp = si12tch_i2c_ReadByte(0); //读取一个字节后，产生NAck
	si12tch_i2c_Stop();
	return temp;
}

/********************************
 * 函数功能：芯片检测
 * 返回值  1：检查失败
 *         0：检查成功
*********************************/
u8 SI12TCH_Check(void)
{
	u8 temp;
	SI12TCH_WriteOneByte(Sense1,0xAA);
	temp=SI12TCH_ReadOneByte(Sense1);
	if(temp==0xAA)
	{
		SI12TCH_WriteOneByte(Sense1,0xFF);
		return 0;
	}
	return 1;
}

/*=================================================================
 读取寄存器output1,2,3数据

	        output3           output2          output1
     |     8bit         |    8bit        |   8bit
     | 00   00  00   00 | 00  00  00  00 | 00  00  00  00
     | t12  t11 t10  t9 | t8  t7  t6  t5 | t4  t3  t2  t1

=====================================================================*/

u32 SI12TCH_ReadData(void)
{
	u32 SI12TCH_data_buf=0;
	u8 data_SI12TCH;
	
	data_SI12TCH=SI12TCH_ReadOneByte(Output3);
	SI12TCH_data_buf|=data_SI12TCH;
	
	SI12TCH_data_buf=SI12TCH_data_buf<<8;
	data_SI12TCH=SI12TCH_ReadOneByte(Output2);
	SI12TCH_data_buf|=data_SI12TCH;
	
	SI12TCH_data_buf=SI12TCH_data_buf<<8;
	data_SI12TCH=SI12TCH_ReadOneByte(Output1);
	SI12TCH_data_buf|=data_SI12TCH;
	
	return SI12TCH_data_buf;	
}

/*=================================================================
 读取寄存器output1,2,3数据

	        output3           output2          output1
     |     8bit         |    8bit        |   8bit
     | 00   00  00   00 | 00  00  00  00 | 00  00  00  00
     | t12  t11 t10  t9 | t8  t7  t6  t5 | t4  t3  t2  t1

=====================================================================*/
u8 data_buf[4] = {0};


void SI12_ReadData(void)
{

	data_buf[0] = SI12TCH_ReadOneByte(Output1);
	
	data_buf[1] = SI12TCH_ReadOneByte(Output2);

	data_buf[2] = SI12TCH_ReadOneByte(Output3);
	
	data_buf[3] = SI12TCH_ReadOneByte(Output4);
	
	
	
}

/********************************
 * 函数功能：芯片硬复位
 *
*********************************/
void SI12TCH_hard_Reset()
{
	GPIO_ResetBits(GPIOB,GPIO_Pin_3);
	delay_ms(10);
	SI12_RST = 1;
	GPIO_SetBits(GPIOB,GPIO_Pin_3);
	delay_ms(10);
	GPIO_ResetBits(GPIOB,GPIO_Pin_3);
	delay_ms(10);
}

/********************************
 * 函数功能  ：SI12TCH初始化
 *
*********************************/
void SI12TCH_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	uint16_t times=0 ; 
	si12tch_iic_Init();
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
	GPIO_Init(GPIOB, &GPIO_InitStructure);
//	IIC_EN = 0 ; 	
//  delay_ms(10);

		
	SI12TCH_hard_Reset();
	delay_ms(200);
	
	
	SI12TCH_WriteOneByte(Ch_hold1,0XFE);     //delay_ms(10); //1111 1110 ch1 =0 通道一打开
	
	SI12TCH_WriteOneByte(CTRL2,0X0b);     
	delay_ms(10); //0000 1111 enable software reset enable sleep mode
	SI12TCH_WriteOneByte(CTRL2,0X07);     
	delay_ms(10); //0000 0111 disable software reset enable sleep mode

	//灵敏度设置
	SI12TCH_WriteOneByte(Sense1,  0x77);     //delay_ms(10); //0111 0111 低灵敏度
	SI12TCH_WriteOneByte(Sense2,  0x77);     //delay_ms(10);
	SI12TCH_WriteOneByte(Sense3,  0x77);     //delay_ms(10);
	SI12TCH_WriteOneByte(Sense4,  0x77);     //delay_ms(10);
	SI12TCH_WriteOneByte(Sense5,  0x77);     //delay_ms(10);
	SI12TCH_WriteOneByte(Sense6,  0x77);     //delay_ms(10);
	//SI12TCH_WriteOneByte(Sense7,  0XFF);     //delay_ms(10);
	
	SI12TCH_WriteOneByte(CTRL1, 0X08);
	SI12TCH_WriteOneByte(Ch_hold1,0Xff);     
	delay_ms(10); //1111 1111 保持操作，不感测不校准
	SI12TCH_WriteOneByte(Ch_hold2,0xff);     
	delay_ms(10); //0011 1111 
	
	SI12TCH_WriteOneByte(Ref_rst1,0X00);     
	delay_ms(10); //0000 0000 禁用参考重置
	SI12TCH_WriteOneByte(Ref_rst2,0X00);     
	delay_ms(10);
	
	SI12TCH_WriteOneByte(Cal_hold1,0Xff);    
	delay_ms(10); //1111 1111 不启用参考校准
	SI12TCH_WriteOneByte(Cal_hold2,0Xff);    
	delay_ms(10);
	
		while(SI12TCH_Check())
	{
		delay_ms(500);
		if(times > 5)
		{
      #if DEBUG
      
      UsartPrintf(USART_DEBUG,"\r\nNO CHECK SI14TCH!\r\n");
      
      #endif
      si12_succced();

			times = 0;
		}
		else times++;
	}
	
		SI12TCH_WriteOneByte(Ch_hold1,0x00);  delay_ms(10);	 //0000 0000 感测校准
		SI12TCH_WriteOneByte(Ch_hold2,0x30);  delay_ms(10);	 //0000 0000 感测校准
	
		SI12TCH_WriteOneByte(Cal_hold1,0x00); delay_ms(10); //0000 0000 启用参考校准
		SI12TCH_WriteOneByte(Cal_hold2,0x00); delay_ms(10); //0000 0000
}
